package com.waterfairy.particle;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author water_fairy
 * @email 995637517@qq.com
 * @date 2020/8/21 17:27
 * @info:
 */
public class FireworksView extends View implements OnFreshListener {
    private static final String TAG = "FireworksView";
    private ObjectMoveEngine objectMoveEngine;


    public FireworksView(Context context) {
        this(context, null);
    }

    public FireworksView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
//        setLayerType(LAYER_TYPE_HARDWARE, null);
        objectMoveEngine = new ObjectMoveEngine();
        objectMoveEngine.setOnFreshListener(this);
        objectMoveEngine.initPaint();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        objectMoveEngine.initSize(right - left, bottom - top);
    }


    public void stop() {
        objectMoveEngine.stop();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        objectMoveEngine.onDraw(canvas);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        objectMoveEngine.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        objectMoveEngine.stop();
    }

    @Override
    public void onFresh() {
        invalidate();
    }

    public void setBitmapList(ArrayList<Bitmap> bitmaps) {
        objectMoveEngine.setBitmapList(bitmaps);
    }
}
